package gui;

import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import client.Main;

public class DelivryDateTime extends JFrame{
	
	JPanel panel;
	
	JButton btnCancel;
	JButton btnOK;
	
	JLabel lblSelectDate;
	JLabel lblDate;
	JLabel lblTime;
	
	JComboBox cmbDay;
	JComboBox cmbMonth;
	JComboBox cmbHour;
	
	public DelivryDateTime()
	{
		super();
		init();
	}
	
	JButton getButtonCancel()
	{
		if(btnCancel==null)
		{
			btnCancel=new JButton();
			btnCancel.setText("Cancel");
			btnCancel.setBounds(140, 120, 80, 30);
			btnCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return btnCancel;
	}
	
	JButton getButtonOK()
	{
		if(btnOK==null)
		{
			btnOK=new JButton();
			btnOK.setText("OK");
			btnOK.setBounds(50, 120, 80, 30);
			btnOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int m=(Integer)cmbMonth.getSelectedItem();
					int d=(Integer)cmbDay.getSelectedItem();
					int h=(Integer)cmbHour.getSelectedItem();
					switch(m)
					{
					case 4:
					case 6:
					case 9:
					case 11:
						if(d>30)
						{
							JOptionPane.showMessageDialog(null, "Illegal Date");
							return;
						}
						break;
					case 2:
						if(d>28)
						{
							JOptionPane.showMessageDialog(null, "Illegal Date");
							return;
						}
						break;
					}

					SimpleDateFormat date=new SimpleDateFormat("dd/MM/yyyy");
					SimpleDateFormat time=new SimpleDateFormat("HH:mm:ss");
					
					Calendar ca=Calendar.getInstance();
					Calendar cb=Calendar.getInstance();
					Calendar c=Calendar.getInstance();
					
					ca.set(Calendar.DAY_OF_MONTH,d);
					ca.set(Calendar.MONTH, m);
					ca.set(Calendar.HOUR, h);
					cb.add(Calendar.HOUR, 3);
					
					if(ca.compareTo(cb) <0)
					{
						JOptionPane.showMessageDialog(null, "Illegal Date");
						return;
					}
					else if(ca.get(Calendar.HOUR)-cb.get(Calendar.HOUR)<0)
					{
						JOptionPane.showMessageDialog(null, "3 hours difference needed");
						return;
					}
					String s[]={date.format(c.getTime()),time.format(c.getTime()),date.format(ca.getTime()),time.format(ca.getTime()) };
					DeliveryGUI.d.setDateTime(s);
					JOptionPane.showMessageDialog(null, "Delivery details saved!");
					dispose();
				}
			});
		}
		return btnOK;
	}
	
	JLabel getLabelDate()
	{
		if(lblDate==null)
		{
			lblDate=new JLabel();
			lblDate.setText("Date (Day/Month)");
			lblDate.setBounds(20, 70, 100, 20);
		}
		return lblDate;
	}
	
	JLabel getLabelTime()
	{
		if(lblTime==null)
		{
			lblTime=new JLabel();
			lblTime.setText("Time (Hour)");
			lblTime.setBounds(150, 70, 100, 20);
		}
		return lblTime;
	}
	
	JComboBox getComboDay()
	{
		if(cmbDay==null)
		{
			cmbDay=new JComboBox();
			for(int i=0;i<31;i++)
				cmbDay.addItem(i+1);
			cmbDay.setBounds(20, 50, 40, 20);
		}
		return cmbDay;
	}
	
	JComboBox getComboMonth()
	{
		if(cmbMonth==null)
		{
			cmbMonth=new JComboBox();
			for(int i=0;i<12;i++)
				cmbMonth.addItem(i+1);
			cmbMonth.setBounds(70, 50, 40, 20);
		}
		return cmbMonth;
	}
	
	JComboBox getComboHour()
	{
		if(cmbHour==null)
		{
			cmbHour=new JComboBox();
			for(int i=0;i<24;i++)
				cmbHour.addItem(i+1);
			cmbHour.setBounds(160, 50, 40, 20);
		}
		return cmbHour;
	}
	
	
	JLabel getLabelSelectDT()
	{
		if(lblSelectDate==null)
		{
			lblSelectDate=new JLabel();
			lblSelectDate.setText("Select Date and Time:");
			lblSelectDate.setBounds(40, 10, 200, 40);
			lblSelectDate.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
		}
		return lblSelectDate;
	}
	
	JPanel getPanel()
	{
		if(panel==null)
		{
			panel=new JPanel();
			panel.setSize(this.getWidth(),this.getHeight());
			panel.setLayout(null);
			
			panel.add(getLabelSelectDT());
			panel.add(getLabelDate());
			panel.add(getLabelTime());
			
			panel.add(getComboDay());
			panel.add(getComboMonth());
			panel.add(getComboHour());
			
			panel.add(getButtonCancel());
			panel.add(getButtonOK());
		}
		return panel;
	}
	
	void init()
	{
		this.setBounds((int)Main.dim.getWidth()/2-150, (int)Main.dim.getHeight()/2-100, 300	, 200);
		this.setTitle("Zer Lah - Delivery Date & Time");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.add(getPanel());
		this.setVisible(true);
	}
}
